//
//  DestinationViewController.m
//  Qyer
//
//  Created by dlios on 15/10/16.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "DestinationViewController.h"

@interface DestinationViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate>

{
    NSInteger _number;
}

@property (nonatomic, strong)UIImageView *mapView;
@property (nonatomic, strong)NSMutableArray *hotCountryArr;

@end

@implementation DestinationViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:34./255 green:169./ 255 blue:118./255 alpha:1];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 签完UIGestureRecognizerDelegate代理,然后写这两句话,效果就是从这个页面push的其他页面都可以右滑返回上一页
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.infoArr = [[NSMutableArray alloc] init];
    self.hotCountryArr = [[NSMutableArray alloc] init];

    [self creatNav];
    [self creatMapViewAndButton];
    [self creatTableView];
    [self afn];
    _number = 0;
}

#pragma mark - 创建假navigation&搜索
- (void)creatNav
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    view.backgroundColor = kCOLOR(34, 169, 118);
    
    UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom];
    search.frame = CGRectMake(50, 25, SCREEN_WIDTH-100, 30);
    search.layer.masksToBounds = YES;
    search.layer.cornerRadius = 10.;
    [search addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    search.backgroundColor = [UIColor colorWithRed:27./255 green:129./255 blue:94./255 alpha:1];
    search.titleLabel.font = [UIFont systemFontOfSize:15.];
    [search setTitle:@"🔍 搜索目的地 : 国家/城市" forState:UIControlStateNormal];
    [view addSubview:search];
    [self.view addSubview:view];
    
}

- (void)search
{
    GYZSearchViewController *search = [[GYZSearchViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:search];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 网络请求
- (void)afn
{
    [AFN getdataWithString:kURL_DESTINATION block:^(id block) {
        if (self.infoArr != nil) {
            [self.infoArr removeAllObjects];
        }
        if (self.hotCountryArr != nil) {
            [self.hotCountryArr removeAllObjects];
        }
        NSDictionary *dataDic = [[block objectForKey:@"data"]objectAtIndex:_number];
        for (NSDictionary *countryDic in [dataDic objectForKey:@"country"]) {
            GYZHot_countryModel *model = [GYZHot_countryModel modelWithDictionary:countryDic];
            [self.infoArr addObject:model];
        }
        
        for (NSDictionary *hotCountryDic in [dataDic objectForKey:@"hot_country"]) {
            GYZHot_countryModel *model = [GYZHot_countryModel modelWithDictionary:hotCountryDic];
            [self.hotCountryArr addObject:model];
        }
        
        [self.tableView reloadData];
        
    } BLOCK:^(id BLOCK) {
    }];
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    // 先判断是否已经创建了table,如果已经有了,就刷新一下就行了,在处理数据里面刷新的
    if (!self.tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 112) style:UITableViewStylePlain];
        self.tableView.tableHeaderView = _mapView;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.view addSubview:self.tableView];
        UILabel *lable = [[UILabel alloc] init];
        self.tableView.tableFooterView = lable;
    }
}

#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArr.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *cellId = @"hotCountryCell";
        GYZHot_countryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[GYZHot_countryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        // 签代理
        [cell setDelegate:self];
        [cell setDataSource:self];
        
        if (_number != 6) {
            
            [cell.collectionView setFrame:CGRectMake(0, 5, SCREEN_WIDTH, SCREEN_WIDTH * 4.3 / 7 * 4.4)];
        }
        else
        {
            [cell.collectionView setFrame:CGRectMake(0, 5, SCREEN_WIDTH, SCREEN_HEIGHT / 7 * 4.4)];
        }
        
        [cell.collectionView reloadData];
        
        return cell;
    }
    else
    {
        static NSString *cellId = @"otherCountryCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        GYZHot_countryModel *model = self.infoArr[indexPath.row - 1];
        cell.textLabel.text = model.cnname;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if (_number != 6) {
            return SCREEN_WIDTH * 4.3 / 7 * 4.4;
        }
        else
        {
            return SCREEN_WIDTH * 1.3/ 7 * 4.4;
        }
    }
    else
    {
        return 50;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 判断 : 城市/旅游地 push到下一页
    GYZHot_countryModel *model = self.infoArr[indexPath.row];
    if ([model.label isEqualToString:@"城市"]) {
        GYZCountryViewController *country = [[GYZCountryViewController alloc] init];
        country.cid = model.cid;
        [self.navigationController pushViewController:country animated:YES];
    }
    else
    {
        GYZCityViewController *city = [[GYZCityViewController alloc] init];
        city.cid = model.cid;
        city.cnname = model.cnname;
        city.enname = model.enname;
        [self.navigationController pushViewController:city animated:YES];
    }
}

#pragma mark - collectionView 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hotCountryArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYZHot_countryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotCountry" forIndexPath:indexPath];
    
    GYZHot_countryModel *model = self.hotCountryArr[indexPath.row];
    [cell setValueWithModel:model];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 判断 : 城市/旅游地 push到下一页
    GYZHot_countryModel *model = _hotCountryArr[indexPath.row];
    if ([model.label isEqualToString:@"城市"]) {
        GYZCountryViewController *country = [[GYZCountryViewController alloc] init];
        country.cid = model.cid;
        [self.navigationController pushViewController:country animated:YES];
    }
    else
    {
        GYZCityViewController *city = [[GYZCityViewController alloc] init];
        city.cid = model.cid;
        city.cnname = model.cnname;
        city.enname = model.enname;
        [self.navigationController pushViewController:city animated:YES];
    }
}

#pragma mark - 世界地图点击方法
// 创建button
- (void)creatMapViewAndButton
{
    _mapView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Asian.png"]];
    _mapView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT / 3);
    [_mapView setUserInteractionEnabled:YES];
    
    [self creatButton:CGRectMake(_mapView.frame.size.width/7*5, _mapView.frame.size.height/6, 50, 35) buttonClick:@selector(Asian:) number:0];
    [self creatButton:CGRectMake(_mapView.frame.size.width/8*4, _mapView.frame.size.height/9, 50, 35) buttonClick:@selector(Europe:) number:1];
    [self creatButton:CGRectMake(_mapView.frame.size.width/9, _mapView.frame.size.height/9, 50, 35) buttonClick:@selector(NorthAmerica:) number:2];
    [self creatButton:CGRectMake(_mapView.frame.size.width/8*2, _mapView.frame.size.height/7*3.5, 50, 35) buttonClick:@selector(SouthAmerica:) number:3];
    [self creatButton:CGRectMake(_mapView.frame.size.width/8*6, _mapView.frame.size.height/7*3.5, 50, 35) buttonClick:@selector(Oceania:) number:4];
    [self creatButton:CGRectMake(_mapView.frame.size.width/7*3+20, _mapView.frame.size.height/6*3-18, 50, 35) buttonClick:@selector(Africa:) number:5];
    [self creatButton:CGRectMake(_mapView.frame.size.width/8*5-5, _mapView.frame.size.height/6*4, 50, 35) buttonClick:@selector(Antarctica:) number:6];
}

- (void)creatButton:(CGRect)rect buttonClick:(SEL)buttonClick number:(NSInteger)number
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = rect;
    [button addTarget:self action:buttonClick forControlEvents:UIControlEventTouchUpInside];
    _number = number;
    [_mapView addSubview:button];
}

// 点击方法
- (void)mapButtonClick:(UIButton *)sender number:(NSInteger)number imageName:(NSString *)imageName
{
    if (sender.selected == NO) {
        [_mapView setImage:[UIImage imageNamed:imageName]];
        
        _number = number;
        [self afn];// 点击后刷新页面
    }
    else{
        sender.selected = YES;
    }
}

- (void)Asian:(UIButton *)sender
{
    [self mapButtonClick:sender number:0 imageName:@"Asian.png"];
}
- (void)Europe:(UIButton *)sender
{
    [self mapButtonClick:sender number:1 imageName:@"Europe.png"];
}
- (void)NorthAmerica:(UIButton *)sender
{
    [self mapButtonClick:sender number:2 imageName:@"NorthAmerica.png"];
}
- (void)SouthAmerica:(UIButton *)sender
{
    [self mapButtonClick:sender number:3 imageName:@"SouthAmerica.png"];
}
- (void)Oceania:(UIButton *)sender
{
    [self mapButtonClick:sender number:4 imageName:@"Oceania.png"];
}
- (void)Africa:(UIButton *)sender
{
    [self mapButtonClick:sender number:5 imageName:@"Africa.png"];
}
- (void)Antarctica:(UIButton *)sender
{
    [self mapButtonClick:sender number:6 imageName:@"Antarctica.png"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
